package com.sxfq.loansupermarket.backsystem.modular.report.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IDimensionService;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IOutpatientManagementService;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.SearchWholeHospitalInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.WholeHospitalInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IWholeHospitalnewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/wholeHospitalnew")
public class WholeHospitalnewController extends BaseController {
    @Autowired
    IDimensionService dimensionService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    IWholeHospitalnewsService service;

    @Autowired
    IOutpatientManagementService outpatientManagementService;

    private String PREFIX = "/report/wholeHospital/";

    /**
     * 跳转到全院报表页面
     */
    @RequestMapping("")
    public String to_wholeHospital(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        model.addAttribute("list", dimensionService.noDepartmentList());
        model.addAttribute("abbreviation", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"");
        model.addAttribute("sttleAccountsTime", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"SttleAccountsTime",0));
        List<Map<String, Object>> outpatientManagementList = outpatientManagementService.lists(ShiroKit.getUser().getAccount());
        model.addAttribute("outpatientManagementList", outpatientManagementList);
        return PREFIX + "wholeHospitalnew.html";
    }

    /**
     * 全院报表列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(SearchWholeHospitalInfo searchInfo, HttpServletRequest request) {
        Page<WholeHospitalInfo> page = new PageFactory<WholeHospitalInfo>().defaultPage();
        try {
            List<WholeHospitalInfo> list = new ArrayList<WholeHospitalInfo>();
            if ("".equals(searchInfo.getDimensionSelection())) {
                list = service.getList(searchInfo);
            } else if ("channel".equals(searchInfo.getDimensionSelection())) {
                list = service.getOneChannelList(searchInfo);
            } else if ("customerAge".equals(searchInfo.getDimensionSelection())) {
                list = service.getCustomerAgeList(searchInfo);
            } else if ("medium".equals(searchInfo.getDimensionSelection())) {
                list = service.getOneMediumList(searchInfo);
            } else if ("tcName".equals(searchInfo.getDimensionSelection())) {
                list = service.getTcNameList(searchInfo);
            } else if ("doctor".equals(searchInfo.getDimensionSelection())) {
                list = service.getDoctorList(searchInfo);
            }
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }
}